package cn.com.bsoft.cloud.dao.cloud;

import cn.com.bsoft.cloud.UseDatasourceCloud;
import cn.com.bsoft.cloud.entity.cloud.PrescriptionDetail;
import cn.com.bsoft.cloud.entity.cloud.PrescriptionMess;
import cn.com.bsoft.cloud.entity.cloud.SendMess;
import cn.com.bsoft.cloud.entity.cloud.Site;
import cn.com.bsoft.cloud.entity.company.CompanyMess;
import cn.com.bsoft.cloud.entity.company.PTCompanyMess;
import cn.com.bsoft.cloud.entity.param.DeliveryParam;
import cn.com.bsoft.cloud.entity.param.DrugListParam;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

@Repository
@UseDatasourceCloud
public interface CloudCompanyDao {

    /**
     * 查询代配药列表
     * @param startDate
     * @param endDate
     * @param hosStr
     * @return
     */
    @Results({@Result(property = "prescriptionCode",column = "prescriptionCode"),
            @Result(property = "cfmess",javaType = List.class,column ="prescriptionCode",
                    many = @Many(select = "cn.com.bsoft.cloud.dao.cloud.CloudCompanyDao.queryDetailByCode"))})
    @Select("select a.pharmacyCode,a.pharmacyName,a.hospitalCode ,a.hospitalName ,\n" +
            "  a.prescriptionCode,a.patientName,a.cardNum,a.extractingDate,\n" +
            "  a.identity,a.sex,a.telphone,b.caSign,b.cert,b.inData\n" +
            "from t_prescription_drug_list a,t_prescription_appointment b\n" +
            "where\n" +
            "  a.extractingDate>#{startDate}\n" +
            "  and a.extractingDate<#{endDate}\n" +
            "  and a.deliveryStatus='0'\n" +
            "  and a.hospitalCode in (${hosStr})\n" +
            "and a.appointId=b.appointId")
    List<PrescriptionMess> queryDrugList(@Param("startDate") Timestamp startDate,@Param("endDate") Timestamp endDate,@Param("hosStr") String hosStr);


    @Select("select drugCode,drugName,specificationCode,specification,dispenseAmount,dispenseUnitsCode,\n" +
            "dispenseUnitsName,frequencyCode,frequencyName,aDose,doseUnitCode,doseUnitName,formulationCode,\n" +
            "formulationName,useDescribe,medicationDay,routeCode,routeName,unitPrice,financialTransaction\n" +
            "from t_prescription_drug_detail \n" +
            "where prescriptionCode=#{prescriptionStr}")
    List<PrescriptionDetail> queryDetailByCode(@Param("prescriptionCode")String prescriptionCode);

    @Update("<script>"+
            "update t_prescription_drug_list\n" +
            "SET deliveryStatus=#{param.status},pharmaceuticalCompanyCode=#{param.pharmaceuticalCompanyCode}," +
            "pharmaceuticalCompanyName=#{param.pharmaceuticalCompanyName},modifytime=DATE_ADD(current_timestamp,INTERVAL 8 HOUR) \n" +
            "WHERE hospitalCode=#{param.hospitalCode} and prescriptionCode in (${param.prescriptionCodeStr})"+
            "<if test='param.pharmacyCode!=null and param.pharmacyCode.length>0 and param.pharmacyName!=null and param.pharmacyName.length>0 '>"+
            "and pharmacyCode=#{param.pharmacyCode} and pharmacyName=#{param.pharmacyName}"+
            "</if>"+
            "</script>")
    Boolean updateDeliveryStatu(@Param("param")DeliveryParam param);


    @Results({
            @Result(column="psdbm", property="siteCode"),
            @Result(column="psdmc", property="siteName"),
            @Result(column="address", property="address")
    })
    @Select("select psdbm,psdmc,address from t_distribution_site where yydm=#{hoscode}")
    List<Site> querySitesByCode(@Param("hoscode") String hoscode);

    @Insert("insert into err_msg values(#{msg},#{message})")
    void saveErrorMess(@Param("msg") String msg, @Param("message") String message);

    @Select("SELECT * FROM t_company_key where companyCode=#{companyCode}")
    List<CompanyMess> queryCompanyMess(@Param("companyCode") String companyCode);

    @Update("update t_company_key SET secret=#{secret},publickey=#{publickey},companyName=#{companyName} where companyCode=#{companyCode}\n")
    Boolean uopdateCompanyMess(@Param("companyCode") String companyCode,@Param("companyName") String companyName,@Param("secret") String secret, @Param("publickey") String publickey);

    @Insert("insert into t_company_key(companyCode,companyName,secret,publickey) values(#{companyCode},#{companyName},#{secret},#{publickey})\n")
    Boolean addCompanyMess(@Param("companyCode") String companyCode,@Param("companyName") String companyName,@Param("secret") String secret, @Param("publickey") String publickey);

    @Select("select companyName from t_company_key where companyCode=#{companyCode}\n")
    String findCompanyNameByCode(@Param("companyCode") String companyCode);

    @Select("select  publickey from t_hos_key")
    String findCompanyPubkey();

    @Select("select publickey from t_company_key where companyCode=#{companyCode}\n")
    String getPublicKey(@Param("companyCode")String companyCode);

    @Select("select  privatekey from t_hos_key")
    String getPrivateKey();

    @Select("select secret from t_company_key where companyCode=#{companyCode}\n")
    String getSecret(@Param("companyCode")String companyCode);


    @Select("select companyCode,companyName,secret,publickey from t_company_key where companyCode=#{companyCode}\n")
    PTCompanyMess getCompanyMess(@Param("companyCode")String companyCode);


    @Results({
            @Result(column="psdbm", property="siteCode"),
            @Result(column="psdmc", property="siteName"),
            @Result(column="address", property="address")
    })
    @Select("select psdbm,psdmc,address from t_distribution_site")
    List<Site> queryAllSiteMess();

    @Update("update t_distribution_site set address=#{address},psdmc=#{siteName} where psdbm=#{siteCode}")
    void updateAddressByCode(@Param("siteCode") String siteCode,@Param("siteName") String siteName,@Param("address") String address);

    @Results({
            @Result(column="yydm", property="hosCode"),
            @Result(column="yymc", property="hosName")
    })
    @Select("select yydm,yymc from t_distribution_site GROUP BY yydm,yymc")
    List<Site> queryHosList();


    @Results({
            @Result(column="yydm", property="hosCode"),
            @Result(column="yymc", property="hosName"),
            @Result(column="psdbm", property="siteCode"),
            @Result(column="psdmc", property="siteName"),
            @Result(column="address", property="address"),
            @Result(column="zfbj", property="zfbj"),
    })
    @Select("select yydm,yymc,psdbm,psdmc,address,zfbj from t_distribution_site where yydm=#{hosCode}")
    List<Site> querySiteByCode(@Param("hosCode") String hosCode);

    @Select("select psdmc from t_distribution_site where psdbm=#{siteCode}")
    String findSiteBySitecode(@Param("siteCode") String siteCode);

    @Insert("insert into t_distribution_site(yydm,yymc,psdbm,psdmc,address,zfbj) values(#{hosCode},#{hosName},#{siteCode},#{siteName},#{address},'0')")
    void addSiteMess(@Param("hosCode")String hosCode, @Param("hosName")String hosName, @Param("siteCode")String siteCode, @Param("siteName")String siteName, @Param("address")String address);

    @Update("update t_distribution_site set zfbj='1' where psdbm=#{siteCode}")
    void delSiteMessByCode(@Param("siteCode") String siteCode);

    @Select("<script>"+
            "select appointId from t_prescription_drug_list\n" +
            "WHERE hospitalCode=#{param.hospitalCode} and prescriptionCode in (${param.prescriptionCodeStr})"+
            "<if test='param.pharmacyCode!=null and param.pharmacyCode.length>0 and param.pharmacyName!=null and param.pharmacyName.length>0 '>"+
            "and pharmacyCode=#{param.pharmacyCode} and pharmacyName=#{param.pharmacyName}"+
            "</if>"+
            "</script>")
    String[] findMessByParam(@Param("param") DeliveryParam param);

    @Select("select a.appointId,a.hospitalName,a.cardNum,b.psdmc,c.address,d.patientName from t_prescription_drug_list a,t_appoint_site b,\n" +
            "t_distribution_site c,t_prescription_appointment d\n" +
            "where prescriptionCode=#{code}\n" +
            "and b.yyid=a.appointId and c.psdbm=b.psdbm and c.yydm=a.hospitalCode and d.appointId=a.appointId")
    SendMess findMessByCode(@Param("code") String code);

   /* @Select("select prescriptionCode\n" +
            "from t_prescription_drug_list\n" +
            "where\n" +
            "extractingDate>#{startDate}\n" +
            "and extractingDate<#{endDate}\n" +
            "and hospitalCode in (${hosStr})")
    List<String> queryprescriptionList(@Param("startDate") Timestamp startDate,@Param("endDate") Timestamp endDate,@Param("hosStr") String hosStr);

    @Select("select drugCode,drugName,specificationCode,specification,dispenseAmount,dispenseUnitsCode,\n" +
            "dispenseUnitsName,frequencyCode,frequencyName,aDose,doseUnitCode,doseUnitName,formulationCode,\n" +
            "formulationName,useDescribe,medicationDay,routeCode,routeName,unitPrice,financialTransaction\n" +
            "from t_prescription_drug_detail \n" +
            "where prescriptionCode IN (${prescriptionStr})")
    List<PrescriptionDetail> queryDetailList(@Param("prescriptionStr")String prescriptionStr);*/



}
